package permission

import (
	"context"
	"github.com/fatih/color"
	"github.com/ifnk/micro-blog/internal/pkg/config"
	"github.com/ifnk/micro-blog/internal/pkg/log"
	"github.com/ifnk/micro-blog/protobuf/permission_pb"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"time"
)

func NewUserClient(logger *log.Logger, conf *config.Config) (permission_pb.UserServiceClient, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 80*time.Second)
	defer cancel()
	target := conf.Permission.Server.Host + conf.Permission.Server.GRPC.Port
	color.Blue("target -> %+v", target)
	conn, err := grpc.DialContext(ctx, target, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		return nil, err
	}
	client := permission_pb.NewUserServiceClient(conn)
	return client, err
}
func NewAuthorityClient(logger *log.Logger, conf *config.Config) (permission_pb.AuthorityServiceClient, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 80*time.Second)
	defer cancel()
	conn, err := grpc.DialContext(ctx, conf.Permission.Server.Host+conf.Permission.Server.GRPC.Port, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		return nil, err
	}
	client := permission_pb.NewAuthorityServiceClient(conn)
	return client, err
}
func NewMenuClient(logger *log.Logger, conf *config.Config) (permission_pb.MenuServiceClient, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 80*time.Second)
	defer cancel()
	conn, err := grpc.DialContext(ctx, conf.Permission.Server.Host+conf.Permission.Server.GRPC.Port, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		return nil, err
	}
	client := permission_pb.NewMenuServiceClient(conn)
	return client, err
}
func NewApiClient(logger *log.Logger, conf *config.Config) (permission_pb.ApiServiceClient, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 80*time.Second)
	defer cancel()
	conn, err := grpc.DialContext(ctx, conf.Permission.Server.Host+conf.Permission.Server.GRPC.Port, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		return nil, err
	}
	client := permission_pb.NewApiServiceClient(conn)
	return client, err
}
func NewUserRltAuthClient(logger *log.Logger, conf *config.Config) (permission_pb.UserRltAuthorityServiceClient, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 80*time.Second)
	defer cancel()
	conn, err := grpc.DialContext(ctx, conf.Permission.Server.Host+conf.Permission.Server.GRPC.Port, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		return nil, err
	}
	client := permission_pb.NewUserRltAuthorityServiceClient(conn)
	return client, err
}
func NewCasbinClient(logger *log.Logger, conf *config.Config) (permission_pb.CasbinServiceClient, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 80*time.Second)
	defer cancel()
	conn, err := grpc.DialContext(ctx, conf.Permission.Server.Host+conf.Permission.Server.GRPC.Port, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		return nil, err
	}
	client := permission_pb.NewCasbinServiceClient(conn)
	return client, err
}
